import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListStackQueue {
    public static void main(String[] args) {
        // 栈
        MyStack<Integer> myStack = new MyStack<Integer>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        myStack.push(6);

        /*
         * while(it.hasNext()){ System.out.println(it.next()); }
         */
        System.out.println("=========栈===========");
        myStack.pop();
        myStack.pop();
        myStack.pop();
        Iterator<Integer> it = myStack.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        // 队列
        MyQueue<Integer> myQueue = new MyQueue<Integer>();
        myQueue.push(100);
        myQueue.push(102);
        myQueue.push(105);
        myQueue.push(106);
        myQueue.push(109);

        myQueue.pop();
        myQueue.pop();
        myQueue.pop();
        System.out.println("=========队列===========");
        Iterator<Integer> iterator = myQueue.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

// 模拟栈功能
class MyStack<T> {
    private LinkedList<T> data = null;

    public MyStack() {
        data = new LinkedList<T>();
    }

    // 压栈
    public void push(T obj) {
        data.addFirst(obj);
    }

    // 出栈
    public T pop() {
        return data.removeFirst();
    }

    public Iterator<T> iterator() {
        return data.iterator();
    }
}

// 模拟队列功能
class MyQueue<T> {
    private LinkedList<T> data = null;

    public MyQueue() {
        data = new LinkedList<T>();
    }

    public void push(T obj) {
        data.addFirst(obj);
    }

    public T pop() {
        return data.removeLast();
    }

    public Iterator<T> iterator() {
        return data.iterator();
    }
}
